package com.demo;

import com.demo.config.SpringConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletRegistration;

/**
 * 替代web.xml的作用，路径随便，没有springboot的向下扫描
 *
 * @author gatts, 2020/8/2 20:05
 */
public class WebInit implements WebApplicationInitializer {

    public void onStartup(javax.servlet.ServletContext servletContext) {
        AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();

        ServletRegistration.Dynamic springmvcServlet = servletContext.addServlet("springmvc", new DispatcherServlet(webApplicationContext));

        springmvcServlet.addMapping("/");

        springmvcServlet.setLoadOnStartup(1);

        webApplicationContext.setServletContext(servletContext);

        webApplicationContext.register(SpringConfig.class);
    }

}
